<?php
/**
 * User: jintao
 * Date: 2016/3/18
 */
class ome_ctl_admin_crm_gift extends desktop_controller
{

    public function rule($act){
         $base_filter = array(
            'shop_id' =>$this->shopIds,
        );
        $this->finder('ome_mdl_gift_rule',array(
            'title'=>'促销规则列表',
            'actions'=>array(
                array(
                    'label'=>'添加促销规则',
                    'href'=>'index.php?app=ome&ctl=admin_crm_gift&act=addAndEdit&p[0]=add&shop_id='.$_GET['shop_id'].'&view='.$view,
                ),
                array(
                    'label'=>'检测是否过期',
                    'href'=>'index.php?app=ome&ctl=admin_crm_gift&act=checkExpire',
                ),
            ),
            'base_filter' => $base_filter,
            'orderBy' => 'status DESC,priority DESC,id DESC',
            'use_buildin_recycle' => false,
        ));
    }

    function _views(){
        if($_GET['act']=='rule'){
            $sub_menu = $this->_viewsAll();
        }
        return $sub_menu;
    }

    function _viewsAll(){
        $mdl_order = $this->app->model('gift_rule');
        $sub_menu = array(
            0 => array('label'=>app::get('base')->_('全部'),'filter'=>array(),'optional'=>false),
            1 => array('label'=>app::get('base')->_('活动中'),'filter'=>array('is_invalid'=>1),'optional'=>false),
            2 => array('label'=>app::get('base')->_('已过期'),'filter'=>array('is_invalid'=>-1),'optional'=>false),
        );
        $i=0;
        foreach($sub_menu as $k=>$v){
            $sub_menu[$k]['filter'] = $v['filter']?$v['filter']:null;
            $sub_menu[$k]['addon'] = $mdl_order->count($v['filter']);
            $sub_menu[$k]['href'] = 'index.php?app=ome&ctl='.$_GET['ctl'].'&act='.$_GET['act'].'&view='.$i++;
        }
        return $sub_menu;
    }

    /**
     * 检测是否过期
     */
    public function checkExpire(){
        $this->begin();
        $oGiftrule = $this->app->model('gift_rule');
        $gifts = $oGiftrule->getList('id,start_time,end_time,title');
        foreach ($gifts as $key => $value) {
            if($value['end_time']<time()){
                error_log(date("Y-m-d H:i:s") . print_r($value['title'], 1) . PHP_EOL, 3, DATA_DIR .'/guoqi.log');
               $oGiftrule->update(array('is_invalid'=>-1),array('id'=>$value['id']));
            }else{
                error_log(date("Y-m-d H:i:s") . print_r($value['title'], 1) . PHP_EOL, 3, DATA_DIR .'/no_guoqi.log');
               $oGiftrule->update(array('is_invalid'=>1),array('id'=>$value['id']));
            }
        }
        $this->end(true, '检测赠品规则过期成功');
    }

   /**
     * 新增/编辑页面显示
     */
    public function addAndEdit(){
        $sales_material_obj = app::get('material')->model('sales_material');
        $shopObj =  app::get('ome')->model('shop');
        $shop_id = $_GET['shop_id'];
        $id = intval($_GET['id']);

        //$shops_name = $shopObj->getList('shop_id,name',array('node_id|noequal'=>''));
        $shops_name = $shopObj->getList('shop_id,name');
        $shops = $shops_name;

        $rule = array(
            'start_time' => strtotime(date('Y-m-d')),
            'status' => 1,
            'shop_id' => $shop_id,
            'time_type' => 'pay_time',
            'lv_id' => 0,
            'filter_arr' => array(
                'order_amount' => array(
                    'type'=>0
                ),
                'buy_goods' => array(
                    'type'=>0
                ),
            ),
        );

        $rs = app::get('eccommon')->model('regions')->getList('local_name',array('region_grade'=>1,'region_id|sthan'=>3320));
        $provinces = $rs;

        $rule_obj = $this->app->model('gift_rule');

        //修改规则信息
        $goods_name = '';
        if($id>0){
            $rule = $rule_obj->dump($id);
            $rule['filter_arr'] = json_decode($rule['filter_arr'], true);

            $goods_bn = $rule['filter_arr']['buy_goods']['goods_bn'];
            $count = count($goods_bn);
            if( ! is_array($goods_bn)){
                $goods_bn = array($goods_bn,'','','','','','','','','');
            }
            $rule['filter_arr']['buy_goods']['goods_bn'] = $goods_bn;

            if($rule['shop_ids']){
                $rule['shop_ids'] = explode(',', $rule['shop_ids']);
            }else{
                $rule['shop_ids'] = array($rule['shop_id']);
            }

            foreach($shops as $ks => &$vs){
                if(in_array($vs['shop_id'], $rule['shop_ids'])){
                    $vs['checked'] = 'true';
                } else {
                    $vs['checked'] = 'false';
                }
            }


            if(isset($rule['filter_arr']['province'])){
                foreach($provinces as $keys => &$vals){
                    if(in_array($vals['local_name'], $rule['filter_arr']['province'])){
                        $vals['checked'] = 'true';
                    } else{
                        $vals['checked'] = 'false';
                    }
                }
            }

            $sm_infos = array();
            foreach($rule['filter_arr']['buy_goods']['goods_bn'] as $kes => $ves){
                $m_objs = $sales_material_obj->dump(array('sales_material_bn' => $ves), 'sm_id');
                $sm_infos[] = $m_objs['sm_id'];
            }

            $this->pagedata['pgid'] = implode(',', $sm_infos);
            $this->pagedata['replacehtml'] = <<<EOF
<div id='hand-selected-product'>已选择了{$count}个销售物料,<a href='javascript:void(0);' onclick='product_selected_show();'>查看选中的销售物料.</a></div>
EOF;

        }else{
            $rule['filter_arr']['buy_goods']['goods_bn'] = array('','','','','','','','','','');
        }

        //已经设定的赠品组合
        $gifts = array();
        if($rule['gift_ids']){
            $gift_ids = explode(',', $rule['gift_ids']);
            $gift_num = explode(',', $rule['gift_num']);

            foreach($gift_ids as $k=>$v){
                $gift_id_num[$v] = $gift_num[$k];
            }

            $gifts = app::get('crm')->model('gift')->getList('*,"checked" as checked',array('gift_id'=>$gift_ids,'gift_num|bthan'=>1));
            foreach($gifts as $k=>$v){
                $gifts[$k]['gift_name'] = mb_substr($v['gift_name'],0,22,'utf-8');
                $gifts[$k]['num'] = $gift_id_num[$v['gift_id']];
            }
        }else{
            //$gifts = $this->app->model('shop_gift')->getList('*',array(),0,5);
        }

        $rule['start_time_hour'] = 0;
        if($rule['start_time']){
            $rule['start_time_hour'] = (int)date('H', $rule['start_time']);
        }

        $rule['end_time_hour'] = 0;
        if($rule['end_time']){
            $rule['end_time_hour'] = (int)date('H', $rule['end_time']);
        }

        $this->pagedata['conf_hours'] = array_merge(array('00','01','02','03','04','05','06','07','08','09'),range(10,23));
        $this->pagedata['provinces'] = $provinces;
        $this->pagedata['goods_name'] = $goods_name;
        $this->pagedata['shops'] = $shops;
        $this->pagedata['gifts'] = $gifts;
        $this->pagedata['rule'] = $rule;
        $this->pagedata['view'] = $_GET['view'];
        $this->pagedata['beigin_time'] = date("Y-m-d",time());
        $this->pagedata['end_time'] = date('Y-m-d',strtotime('+15 days'));
        $this->page('admin/gift/rule_edit.html');
    }

    public function logs(){
        $actions = array();
        $base_filter = array();
        if(!empty($this->shopIds) && isset($this->shopIds)){
            $base_filter['shop_id'] = $this->shopIds;
        }
        $this->finder('ome_mdl_gift_logs',array(
            'title'=>'赠品发送记录',
            'actions'=>$actions,
            'base_filter'=>$base_filter,
            'orderBy' => 'id DESC',
            'use_buildin_export'=>true,  //sxl
            'use_buildin_recycle' => false,
            'use_view_tab' => true,
            'use_buildin_filter'=>true,
        ));
    }

    // public function setgift(){
    //     //是否开启本地赠品规则
    //     $gift_on_off = $this->app->getConf('gift.on_off.cfg');
    //     $this->pagedata['gift_on_off'] = $gift_on_off;
    //     //出错处理方式
    //     $gift_error_ways = $this->app->getConf('gift.error.ways');
    //     $this->pagedata['gift_error_ways'] = $gift_error_ways;
    //     $this->page('admin/gift/set.html');
    // }

     //赠品设置操作和显示
    public function setgift(){
        //判断是否保存操作
        if(isset($_POST['set_gift_erp'])){
            $set_gift_taobao = $set_gift_erp = 'on';
            //淘宝赠品启用状态
            if(empty($_POST['set_gift_taobao']) || $_POST['set_gift_taobao'] == 'off' ){
                $set_gift_taobao = 'off';
                app::get('ome')->setConf('ome.preprocess.tbgift','false');
            }else{
                app::get('ome')->setConf('ome.preprocess.tbgift','true');
            }
            //本地赠品启用状态
            if(empty($_POST['set_gift_erp']) || $_POST['set_gift_erp'] == 'off' ){
                $set_gift_erp = 'off';
                $this->app->setConf('gift.on_off.cfg', 'off');
            }else{
                $this->app->setConf('gift.on_off.cfg', 'on');
            }
            //本地赠品的出错处理
            if(empty($_POST['erp_gift_error_setting']) || $_POST['erp_gift_error_setting'] == 'off'){
                //关闭出错,审单发货
                $this->app->setConf('gift.error.ways', 'off');
            }else{
                $this->app->setConf('gift.error.ways', 'on');
            }
            $arr = array(
                    'set_gift_taobao'=>$set_gift_taobao,
                    'set_gift_erp'=>$set_gift_erp,
                    'op_user' => kernel::single('desktop_user')->get_name(),
                    'create_time' => time(),
            );
            $url = 'index.php?app=ome&ctl=admin_crm_gift&act=setgift';
            $this->begin($url);
            $this->app->model('gift_set_logs')->insert($arr);
            $this->end(true,'保存成功');
        }
        
        //是否启用淘宝赠品(兼容很早以前的)
        $set_gift_taobao = 'off';
        $taobao_gift_setting = app::get('ome')->getConf('ome.preprocess.tbgift');
        if($taobao_gift_setting == 'true'){
            $set_gift_taobao = 'on';
        }
        
        $this->pagedata['set_gift_taobao'] = $set_gift_taobao;
        $this->pagedata['set_gift_erp'] = $this->app->getConf('gift.on_off.cfg');
        $this->pagedata['erp_gift_error_setting'] = $this->app->getConf('gift.error.ways');
        
        $extra_view = array('ome'=>'admin/gift/set.html');
        
        $actions = array();
        $base_filter = array();
        $this->finder('ome_mdl_gift_set_logs',array(
                'title'=>'赠品设置',
                'actions'=>$actions,
                'base_filter'=>$base_filter,
                'orderBy' => 'id DESC',
                'use_buildin_recycle' => false,
                'use_buildin_filter' => false,
                'use_view_tab' => false,
                'top_extra_view' => $extra_view,
                'use_buildin_setcol' => false,
                'use_buildin_refresh' => false,
        ));
        
    }

    /**
     * 规则保存
     */
    public function save_rule(){

        $this->begin('index.php?app=ome&ctl=admin_crm_gift&act=rule');
        $gift_rule_obj = app::get('ome')->model('gift_rule');
        $sales_material_obj = app::get('material')->model('sales_material');

        $data = $_POST;
        $data['filter_arr'] = $_POST['filter_arr'];
        $data['gift_ids'] = $_POST['gift_id'];
        $data['gift_num'] = $_POST['gift_num'];
        $data['shop_ids'] = $_POST['shop_ids'];
        $data['start_time'] = strtotime($_POST['start_time'].' '.$_POST['start_time_hour'].':00:00');
        $data['end_time'] = strtotime($_POST['end_time'].' '.$_POST['end_time_hour'].':00:00');
        $data['modified_time'] = time();

        if(is_array($data['shop_ids']) && count($data['shop_ids'])>10){
            $this->end(false, '最多只能选择十个店铺');
        }

        $data['shop_id'] = $data['shop_ids'][0];
        $data['shop_ids'] = empty($data['shop_ids']) ? '' : implode(',', $data['shop_ids']);

        //if($data['filter_arr']['buy_goods']['goods_bn']){
        //    foreach($data['filter_arr']['buy_goods']['goods_bn'] as $v){
        //        $v = strtoupper($v);
        //    }
        //}

        $sales_material_bns = array();
        if(!empty($data['sm_id'])){
            foreach($data['sm_id'] as $ka => $va){
                $sales_material_bns[] = $sales_material_obj->dump(array('sm_id' => $va), 'sales_material_bn');
            }
        }

        $data['filter_arr']['buy_goods']['goods_bn'] = array();
        foreach($sales_material_bns as $k => $v){
            $data['filter_arr']['buy_goods']['goods_bn'][] = $v['sales_material_bn'];
        }

        $data['filter_arr'] = json_encode($data['filter_arr']);

        if(!$data['id']) $data['create_time'] = time();

        //清理gift_num
        foreach($data['gift_num'] as $k=>$v){
            if(!in_array($k, $data['gift_ids'])){
                unset($data['gift_num'][$k]);
            }
        }

        $data['gift_ids'] = implode(',', $data['gift_ids']);
        $data['gift_num'] = implode(',', $data['gift_num']);

        if($data['id']){
            // 数据快照
            $obj_service = kernel::servicelist('data.snapshot');
            foreach($obj_service as $obj){
                if(method_exists($obj, 'gift_rule_change')){
                    $sdf = $gift_rule_obj->dump($data['id'], '*');
                    $sdf = array(
                        'title' => '赠品规则发生变动',
                        'content' => json_encode($sdf),
                        'task_id' => $data['id'],
                    );
                    $obj->gift_rule_change($sdf);
                }
            }
        }

        if($gift_rule_obj->save($data)){
            $this->end(true, '添加成功');
        } else {
            $this->end(false, '添加失败');
        }

    }

    public function priority($id=0){
        if($_POST){
            $this->begin("index.php?app=ome&ctl=admin_crm_gift&act=rule");
            $shopGiftObj = app::get('ome')->model('gift_rule');
            $data = $_POST;
            $data['priority'] = intval($_POST['priority']);
            $data['modified_time'] = time();
            if($shopGiftObj->save($data)){
                $this->end(true,'添加成功');
            }else{
                $this->end(false,'添加失败');
            }
        }

        //修改规则信息
        if($id>0){
            $rule = $this->app->model('gift_rule')->dump($id);

            $rule['start_time'] = date("Y-m-d", $rule['start_time']);
            $rule['end_time'] = date("Y-m-d", $rule['end_time']);
        }

        $this->pagedata['rule'] = $rule;
        $this->pagedata['view'] = $_GET['view'];
        $this->display('admin/gift/priority.html');
    }


    function ajax_set_derail(){
        $derail = $_POST['on_or_off'];
        $rst = $this->app->setConf('gift.on_off.cfg', $derail);
        echo json_encode($rst);
    }

    function ajax_set_dealway(){
        $ways = $_POST['dealways'];
        $rst = $this->app->setConf('gift.error.ways', $ways);
        echo json_encode($rst);
    }

    function object_rows(){

        if($_POST['data']){
            if($_POST['app_id'])
                $app = app::get($_POST['app_id']);
            else
                $app = $this->app;
            $obj = $app->model($_POST['object']);
            $schema = $obj->get_schema();
            $textColumn = $_POST['textcol']?$_POST['textcol']:$schema['textColumn'];
            $textColumn = explode(',',$textColumn);
            $_textcol = $textColumn;
            $textColumn = $textColumn[0];

            $keycol = $_POST['key']?$_POST['key']:$schema['idColumn'];


            //统一做掉了。
            if( $_POST['data'][0]==='_ALL_' ) {
                if (isset($_POST['filter']['advance']) && $_POST['filter']['advance']){
                    $arr_filters = explode(',',$_POST['filter']['advance']);
                    foreach ($arr_filters as $obj_filter)
                    {
                        $arr    = explode('=', $obj_filter);
                        $_POST['filter'][$arr[0]]    = $arr[1];
                    }
                    unset($_POST['filter']['advance']);
                }

                $all_filter = !empty($obj->__all_filter) ? $obj->__all_filter : array();
                $filter = !empty($_POST['filter']) ? $_POST['filter'] : $all_filter;
                $arr_list = $obj->getList( $keycol,$filter );
                $_POST['data'] = array_map('current',$arr_list);
            }

            $items = $obj->getList('*', array($keycol=>$_POST['data']));
            $name = $items[0][$textColumn];
            if($_POST['type']=='radio'){
                if(strpos($textColumn,'@')!==false){
                    list($field,$table,$app_) = explode('@',$textColumn);
                    if($app_){
                        $app = app::get($app_);
                    }
                    $mdl = $app->model($table);
                    $schema = $mdl->get_schema();
                    $row = $mdl->getList('*',array($schema['idColumn']=>$items[0][$keycol]));
                    $name = $row[0][$field];

                }
                echo json_encode(array('id'=>$items[0][$keycol],'name'=>$name));
                exit;
            }

            $this->pagedata['_input'] = array('items'=>$items,
                'idcol' => $schema['idColumn'],
                'keycol' => $keycol,
                'textcol' => $textColumn,
                '_textcol' => $_textcol,
                'name'=>$_POST['name']
            );
            $this->pagedata['_input']['view_app'] = 'desktop';
            $this->pagedata['_input']['view'] = $_POST['view'];
            if($_POST['view_app']){
                $this->pagedata['_input']['view_app'] =  $_POST['view_app'];
            }

            if(strpos($_POST['view'],':')!==false){
                list($view_app,$view) = explode(':',$_POST['view']);
                $this->pagedata['_input']['view_app'] = $view_app;
                $this->pagedata['_input']['view'] = $view;

            }

            $this->display('admin/gift/input-row.html');
        }
    }

    function finder_common(){
        $params = array(
            'title'=>app::get('desktop')->_('列表'),
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
            'use_buildin_setcol'=>true,
            'use_buildin_refresh'=>true,
            'finder_aliasname'=>'finder_common',
            'alertpage_finder'=>true,
            'use_buildin_tagedit'=>false,
        );
        if ($_GET['findercount']) {
            $params['object_method']['count'] = $_GET['findercount'];
        }
        if ($_GET['findergetlist']) {
            $params['object_method']['getlist'] = $_GET['findergetlist'];
        }
        $params['slaes_material_type'] = $_GET['slaes_material_type'];
        if(substr($_GET['name'],0,7) == 'adjunct') $params['orderBy'] = 'goods_id desc';
        $this->finder($_GET['app_id'].'_mdl_'.$_GET['object'],$params);
    }

    /**
     * @description 显示选用的货品
     * @access public
     * @param void
     * @return void
     */
    public function showProducts()
    {
        $salesMaterialObj = app::get('material')->model('sales_material');#销售物料 ExBOY

        $sm_id = kernel::single('base_component_request')->get_post('sm_id');

        if ($sm_id)
        {
            #前端店铺_规则应用传值 ExBOY
            if(!is_array($sm_id))
            {
                $sm_id    = explode(',', $sm_id);
            }

            $this->pagedata['_input'] = array(
                'name' => 'sm_id',
                'idcol' => 'sm_id',
                '_textcol' => 'sales_material_name',
            );

            $list    = $salesMaterialObj->getList('sm_id,sales_material_name,sales_material_bn', array('sm_id'=>$sm_id, 'sales_material_type|noequal'=>3),0,-1,'sm_id asc');

            $this->pagedata['_input']['items'] = $list;
        }

        $this->display('admin/gift/show_products.html');
    }

    /**
     * @description AJax加载选择销售物料模板
     * @param $id    int
     * @param $shop_ids    String
     * @return void
     */
    public function ajax_sales_material_html($id='', $shop_ids='')
    {
        #规则应用详细信息
        if($id)
        {
            $sales_material_obj = app::get('material')->model('sales_material');
            $gift_obj = app::get('ome')->model('gift_rule');
            $data = $gift_obj->dump(array('id' => $id));

            $gift_rule = json_decode($data['filter_arr'], true);

            $sm_infos = array();
            foreach($gift_rule['buy_goods']['goods_bn'] as $ks => $vs){
                $m_objs = $sales_material_obj->dump(array('sales_material_bn' => $vs));
                $sm_infos[] = $m_objs['sm_id'];
            }

            $googs_bns = implode(',', $sm_infos);

            # 获取ID范围
            if ($googs_bns && $googs_bns != '_ALL_') {

                $data['pgid'] = explode(',',$googs_bns);

                $count = count($data['pgid']);
                $this->pagedata['replacehtml'] = <<<EOF
<div id='hand-selected-product'>已选择了{$count}个销售物料,<a href='javascript:void(0);' onclick='product_selected_show();'>查看选中的销售物料.</a></div>
EOF;
            }
        }

        #店铺
        $shop_id    = array();
        if($shop_ids && $shop_ids != '_ALL_')
        {
            $shop_id_list    = explode(',', $shop_ids);
            foreach ($shop_id_list as $key => $val)
            {
                if(empty($shop_id_list))
                {
                    unset($shop_id_list[$key]);
                }
            }

            $shop_id    = $this->app->model('shop')->getList('shop_id', array('shop_id'=>$shop_id_list));
            $shop_id    = array_map('current', $shop_id);
        }

        $this->pagedata['data'] = $data;
        $this->pagedata['pgid'] = $googs_bns;
        $this->pagedata['in_shop_id'] = ($shop_id ? implode(',', $shop_id).',_ALL_' : '');
        $this->display('admin/gift/select_sales_material.html');
    }

}
